using System;
using System.IO;

namespace WxLib.Utility
{
    public class DefaultFileLoger : ILogHelper
    {
        private const string LOG_FILE = "log.log";
        private const string ERROR_FILE = "err.log";
        public void WriteError(string error)
        {
            string fullPath = Path.Combine(AppContext.BaseDirectory, ERROR_FILE);
            WriteAsync(fullPath, error);
        }

        public void WriteLog(string log)
        {
            string fullPath = Path.Combine(AppContext.BaseDirectory, LOG_FILE);
            WriteAsync(fullPath, log);
        }

        private async void WriteAsync(string fullPath, string logText)
        {
            using (FileStream stream = new FileStream(fullPath, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.AutoFlush = true;
                    await writer.WriteLineAsync(logText);
                }
            }
        }
    }
}